package com.pick.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pick.admin.entity.SysPermission;
import com.pick.admin.entity.SysRolePermission;
import com.pick.admin.mapper.SysPermissionMapper;
import com.pick.admin.mapper.SysRolePermissionMapper;
import com.pick.admin.service.SysPermissionService;
import com.pick.base.common.PageQuery;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统权限服务实现类
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements SysPermissionService {

    @Autowired
    private SysRolePermissionMapper rolePermissionMapper;

    @Override
    public IPage<SysPermission> pagePermissions(PageQuery pageQuery) {
        Page<SysPermission> page = new Page<>(pageQuery.getCurrent(), pageQuery.getSize());
        LambdaQueryWrapper<SysPermission> wrapper = new LambdaQueryWrapper<>();
        
        if (StrUtil.isNotBlank(pageQuery.getKeyword())) {
            wrapper.like(SysPermission::getPermissionName, pageQuery.getKeyword())
                    .or().like(SysPermission::getPermissionCode, pageQuery.getKeyword())
                    .or().like(SysPermission::getDescription, pageQuery.getKeyword());
        }
        
        if (StrUtil.isNotBlank(pageQuery.getOrderBy())) {
            if ("ASC".equalsIgnoreCase(pageQuery.getOrder())) {
                wrapper.orderByAsc(SysPermission::getId);
            } else {
                wrapper.orderByDesc(SysPermission::getId);
            }
        } else {
            wrapper.orderByDesc(SysPermission::getCreateTime);
        }
        
        return this.page(page, wrapper);
    }

    @Override
    public boolean createPermission(SysPermission permission) {
        // 检查权限编码是否已存在
        long count = this.count(new LambdaQueryWrapper<SysPermission>()
                .eq(SysPermission::getPermissionCode, permission.getPermissionCode()));
        if (count > 0) {
            throw new RuntimeException("权限编码已存在");
        }
        
        permission.setStatus(1); // 默认启用
        return this.save(permission);
    }

    @Override
    public boolean updatePermission(SysPermission permission) {
        // 检查权限编码是否已存在（排除自己）
        long count = this.count(new LambdaQueryWrapper<SysPermission>()
                .eq(SysPermission::getPermissionCode, permission.getPermissionCode())
                .ne(SysPermission::getId, permission.getId()));
        if (count > 0) {
            throw new RuntimeException("权限编码已存在");
        }
        
        return this.updateById(permission);
    }

    @Override
    @Transactional
    public boolean deletePermission(Long permissionId) {
        // 检查是否有角色使用该权限
        long roleCount = rolePermissionMapper.selectCount(new LambdaQueryWrapper<SysRolePermission>()
                .eq(SysRolePermission::getPermissionId, permissionId));
        if (roleCount > 0) {
            throw new RuntimeException("该权限下还有角色，无法删除");
        }
        
        // 删除权限
        return this.removeById(permissionId);
    }
}